package hn.cch.security.controller;


import hn.cch.security.entity.Role;
import hn.cch.security.repository.RoleRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.security.Principal;

@Controller
@RequestMapping("/role")
public class RoleController {
    private static Logger logger = LoggerFactory.getLogger(RoleController
            .class);

    @Autowired
    private RoleRepository roleRepository;

    @RequestMapping("/index")
    public String index(ModelMap model, Principal user) throws Exception {
        model.addAttribute("user", user);
        return "role/index";
    }

    @RequestMapping(value = "/{id}")
    public String show(ModelMap model, @PathVariable Long id) {
        Role role = roleRepository.findOne(id);
        model.addAttribute("role", role);
        return "role/show";
    }

    @RequestMapping(value = "/list")
    @ResponseBody
    public Page<Role> getList(Role role) {

        Pageable pageable = new PageRequest(0, 4, new Sort(Sort.Direction
                .ASC, "id"));
        return roleRepository.findPageByName(role.getName() == null ? "%"
                : "%" + role.getName() + "%", pageable);

    }

    @RequestMapping("/new")
    public String create() {
        return "role/new";
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public String save(Role role) throws Exception {
        roleRepository.save(role);
        logger.info("新增->ID=" + role.getId());
        return "1";
    }

    @RequestMapping(value = "/edit/{id}")
    public String update(ModelMap model, @PathVariable Long id) {
        Role role = roleRepository.findOne(id);
        model.addAttribute("role", role);
        return "role/edit";
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public String update(Role role) throws Exception {
        roleRepository.save(role);
        logger.info("修改->ID=" + role.getId());
        return "1";
    }

    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    @ResponseBody
    public String delete(@PathVariable Long id) throws Exception {
        roleRepository.delete(id);
        logger.info("删除->ID=" + id);
        return "1";
    }

}
